<template>
  <div class="checkout-address">
    <div class="text">
      <div v-if="!showAddress" class="none">您需要先添加收货地址才可提交订单。</div>
      <ul v-if="showAddress">
        <li>
          <span>收<i />货<i />人：</span>{{ showAddress.receiver }}
        </li>
        <li><span>联系方式：</span>{{ showAddress.contact.replace(/^(\d{3})(\d{4})(\d{4})/, '$1****$2') }}</li>
        <li><span>收货地址：</span>{{ showAddress.fullLocation }}{{ showAddress.address }}</li>
      </ul>
      <a @click="openAddressEdit(showAddress)" v-if="showAddress" href="javascript:;">修改地址</a>
    </div>
    <div class="action">
      <XtxButton @click="openDialog" class="btn">切换地址</XtxButton>
      <XtxButton @click="openAddressEdit({})" class="btn">添加地址</XtxButton>
    </div>
  </div>
  <!-- 对话框组件 -->
  <XtxDialog title="切换收货地址" v-model:visiable="dialogVisiable">
    <div @click="selectedAddress = item" :class="{ active: selectedAddress?.id === item.id }" class="text item" v-for="item in list" :key="item.id">
      <ul>
        <li>
          <span>收<i />货<i />人：</span>{{ item.receiver }}
        </li>
        <li><span>联系方式：</span>{{ item.contact }}</li>
        <li><span>收货地址：</span>{{ item.fullLocation.replace(/ /g, '') + item.address }}</li>
      </ul>
    </div>
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-right: 20px" @click="dialogVisiable = false">取消</XtxButton>
      <XtxButton type="primary" @click="confirmAddress()">确认</XtxButton>
    </template>
  </XtxDialog>
  <!-- 添加地址组件和修改地址组件 -->
  <AddressEdit ref="addressEditCom" @onSuccess="successHandler" />
</template>

<script>
import { ref } from 'vue'
import AddressEdit from './address-edit.vue'
export default {
  components: { AddressEdit },
  name: 'checkOutAddress',
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  setup(props, { emit }) {
    // 1.找到默认的收货地址  defultAddress
    // 2.没有默认地址选择第一条做默认地址
    // 3.如果没有数据应该显示让用户添加地址的界面
    const showAddress = ref(null)
    const defaultAddress = props.list.find((item) => item.isDefault === 0)
    if (defaultAddress) {
      showAddress.value = defaultAddress
    } else {
      if (props.list.length) {
        // eslint-disable-next-line vue/no-setup-props-destructure
        showAddress.value = props.list[0]
      }
    }

    // 控制对话框的显示和隐藏
    const dialogVisiable = ref(false)

    // 向父组件传递要显示地址的id
    emit('change', showAddress.value && showAddress.value.id)

    // 选择的地址 点击之后就将整个地址信息交给selectedAddress
    const selectedAddress = ref(null)
    //  点击修改地址,打开对话框
    const openDialog = () => {
      dialogVisiable.value = true
      selectedAddress.value = null
    }
    // 点击确认 地址内容要修改  还要保存地址的id 提交的时候使用
    const confirmAddress = () => {
      dialogVisiable.value = false
      showAddress.value = selectedAddress.value
      emit('change', selectedAddress.value.id)
    }

    // 点击添加地址显示地址框  给子组件设置ref属性  获取到子组件可以调用子组件中的方法
    const addressEditCom = ref(null)
    const openAddressEdit = (address) => {
      addressEditCom.value.open(address)
    }

    // 事件
    const successHandler = (formData) => {
      // 判断是更改地址信息还是新增地址信息
      const editAddress = props.list.find((item) => item.id === formData.id)
      if (editAddress) {
        for (const key in editAddress) {
          editAddress[key] = formData[key]
        }
      } else {
        // 要对对象进行拷贝
        const json = JSON.stringify(formData)
        // eslint-disable-next-line vue/no-mutating-props
        props.list.unshift(JSON.parse(json))
      }
    }
    return { showAddress, dialogVisiable, selectedAddress, openDialog, confirmAddress, addressEditCom, openAddressEdit, successHandler }
  }
}
</script>

<style lang="less" scoped>
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
